package com.meiyu.live.ui.columnfollow;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.eventbus.UpdateRecommendEvent;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.param.FocusEditParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.net.response.FocusResponse;
import com.meiyu.live.popj.Assort;
import com.meiyu.live.ui.main.RecommendFragment;
import com.meiyu.live.util.DisplayUtils;
import com.meiyu.live.widget.NetLoadingDailog;
import com.meiyu.live.widget.XCFlowLayout;

import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * Created by zbl on 2016/8/17.
 */
public class ColumnFollowActivity extends BaseActivity implements View.OnClickListener {

    protected NetLoadingDailog dialog_loading;
    private View tab_hot, tab_all, btn_back;
    private ImageView iv_indicator0, iv_indicator1;
    private XCFlowLayout flowLayout_followed, flowLayout_hot, flowLayout_all;
    private ViewGroup.MarginLayoutParams lp;

    private FocusResponse focusResponse;
    private HashMap<Long, Assort> followedColumns = new HashMap<>();
    private boolean isDataChanged = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_columnfollow);
        init();
    }

    private void init() {
        lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = DisplayUtils.dpToPixel(this, 5);
        lp.rightMargin = lp.leftMargin;
        lp.topMargin = lp.rightMargin;
        lp.bottomMargin = lp.topMargin;

        dialog_loading = new NetLoadingDailog(this);

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);

        tab_hot = findViewById(R.id.layout_tab_hot);
        tab_hot.setOnClickListener(this);
        tab_all = findViewById(R.id.layout_tab_all);
        tab_all.setOnClickListener(this);
        iv_indicator0 = (ImageView) findViewById(R.id.iv_indicator0);
        iv_indicator1 = (ImageView) findViewById(R.id.iv_indicator1);
        flowLayout_followed = (XCFlowLayout) findViewById(R.id.flowlayout_followed);
        flowLayout_hot = (XCFlowLayout) findViewById(R.id.flowlayout_hot);
        flowLayout_all = (XCFlowLayout) findViewById(R.id.flowlayout_all);

        getData();
        getFollowedColumn();
    }

    private void getFollowedColumn() {
        followedColumns.putAll(RecommendFragment.getFollowedColumn());
        for (Assort bean : followedColumns.values()) {
            bean.isSelected = true;
            ColumnFollowView view = new ColumnFollowView(this);
            view.setOnClickListener(onClickListener_followed);
            view.setData(bean);
            flowLayout_followed.addView(view, lp);
        }
    }

    private void getData() {
        dialog_loading.loading();
        request(new BaseBodyParams(Constant.UrlParams.FOCUS), FocusResponse.class, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse baseResponse) {
                focusResponse = (FocusResponse) baseResponse;
                updateFlowlayout();
                dialog_loading.dismissDialog();
            }
        });
    }

    private void updateFlowlayout() {
        if (focusResponse != null && focusResponse.getAll_assorts() != null) {
            for (Assort bean : focusResponse.getAll_assorts()) {
                bean.isSelected = followedColumns.containsKey(bean.getId());
                ColumnFollowView view = new ColumnFollowView(this);
                view.setOnClickListener(onClickListener_all);
                view.setData(bean);
                flowLayout_all.addView(view, lp);
            }
            for (Assort bean : focusResponse.getHot_assorts()) {
                bean.isSelected = followedColumns.containsKey(bean.getId());
                ColumnFollowView view = new ColumnFollowView(this);
                view.setOnClickListener(onClickListener_hot);
                view.setData(bean);
                flowLayout_hot.addView(view, lp);
            }
        }
    }

    private View.OnClickListener onClickListener_followed = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            isDataChanged = true;
            ColumnFollowView followView = (ColumnFollowView) view;
            followedColumns.remove(followView.getData().getId());
            flowLayout_followed.removeView(followView);
            for (int i = 0; i < flowLayout_hot.getChildCount(); i++) {
                final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_hot.getChildAt(i);
                if (columnFollowView.getData().getId() == followView.getData().getId()) {
                    columnFollowView.getData().isSelected = false;
                    columnFollowView.update();
                }
            }
            for (int i = 0; i < flowLayout_all.getChildCount(); i++) {
                final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_all.getChildAt(i);
                if (columnFollowView.getData().getId() == followView.getData().getId()) {
                    columnFollowView.getData().isSelected = false;
                    columnFollowView.update();
                }
            }
        }
    };

    private View.OnClickListener onClickListener_hot = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            isDataChanged = true;
            ColumnFollowView followView = (ColumnFollowView) view;
            followView.getData().isSelected = !followView.getData().isSelected;
            followView.update();
            if (followView.getData().isSelected) {
                followedColumns.put(followView.getData().getId(), followView.getData());
                ColumnFollowView cView = new ColumnFollowView(ColumnFollowActivity.this);
                cView.setOnClickListener(onClickListener_followed);
                cView.setData(followView.getData());
                flowLayout_followed.addView(cView, lp);


                for (int i = 0; i < flowLayout_all.getChildCount(); i++) {
                    final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_all.getChildAt(i);
                    if (columnFollowView.getData().getId() == followView.getData().getId()) {
                        columnFollowView.getData().isSelected = true;
                        columnFollowView.update();
                    }
                }
            } else {
                for (int i = 0; i < flowLayout_followed.getChildCount(); i++) {
                    final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_followed.getChildAt(i);
                    if (columnFollowView.getData().getId() == followView.getData().getId()) {
                        columnFollowView.getData().isSelected = false;
                        flowLayout_followed.removeView(columnFollowView);
                    }
                }

                for (int i = 0; i < flowLayout_all.getChildCount(); i++) {
                    final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_all.getChildAt(i);
                    if (columnFollowView.getData().getId() == followView.getData().getId()) {
                        columnFollowView.getData().isSelected = false;
                        columnFollowView.update();
                    }
                }
            }
        }
    };

    private View.OnClickListener onClickListener_all = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            isDataChanged = true;
            ColumnFollowView followView = (ColumnFollowView) view;
            followView.getData().isSelected = !followView.getData().isSelected;
            followView.update();

            if (followView.getData().isSelected) {
                followedColumns.put(followView.getData().getId(), followView.getData());
                ColumnFollowView cView = new ColumnFollowView(ColumnFollowActivity.this);
                cView.setOnClickListener(onClickListener_followed);
                cView.setData(followView.getData());
                flowLayout_followed.addView(cView, lp);


                for (int i = 0; i < flowLayout_hot.getChildCount(); i++) {
                    final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_hot.getChildAt(i);
                    if (columnFollowView.getData().getId() == followView.getData().getId()) {
                        columnFollowView.getData().isSelected = true;
                        columnFollowView.update();
                    }
                }
            } else {
                for (int i = 0; i < flowLayout_followed.getChildCount(); i++) {
                    final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_followed.getChildAt(i);
                    if (columnFollowView.getData().getId() == followView.getData().getId()) {
                        columnFollowView.getData().isSelected = false;
                        flowLayout_followed.removeView(columnFollowView);
                    }
                }

                for (int i = 0; i < flowLayout_hot.getChildCount(); i++) {
                    final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_hot.getChildAt(i);
                    if (columnFollowView.getData().getId() == followView.getData().getId()) {
                        columnFollowView.getData().isSelected = false;
                        columnFollowView.update();
                    }
                }
            }
        }
    };

    @Override
    public void onClick(View view) {
        if (view == tab_hot) {
            iv_indicator0.setImageResource(R.color.main_theme_color);
            flowLayout_hot.setVisibility(View.VISIBLE);
            iv_indicator1.setImageResource(R.color.trans);
            flowLayout_all.setVisibility(View.GONE);
        } else if (view == tab_all) {
            iv_indicator0.setImageResource(R.color.trans);
            flowLayout_hot.setVisibility(View.GONE);
            iv_indicator1.setImageResource(R.color.main_theme_color);
            flowLayout_all.setVisibility(View.VISIBLE);
        } else if (view == btn_back) {
            saveAndFinish();
        }
    }

    @Override
    public void onBackPressed() {
        saveAndFinish();
    }

    private void saveAndFinish() {
        if(!isDataChanged){
            finish();
            return;
        }
        dialog_loading.loading();
        long a[] = new long[flowLayout_followed.getChildCount()];
        for (int i = 0; i < flowLayout_followed.getChildCount(); i++) {
            final ColumnFollowView columnFollowView = (ColumnFollowView) flowLayout_followed.getChildAt(i);
            a[i] = columnFollowView.getData().getId();
        }
        request(new FocusEditParams(a), BaseResponse.class, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse baseResponse) {
                EventBus.getDefault().post(new UpdateRecommendEvent());
                dialog_loading.dismissDialog();
                finish();
            }
        });
    }

    @Override
    public void onError() {
        dialog_loading.dismissDialog();
        super.onError();
    }
}
